package com.dmg.sp.controller;


import com.dmg.sp.entity.MyUserDetails;
import com.dmg.sp.entity.Role;
import com.dmg.sp.entity.User;
import com.dmg.sp.service.RoleService;
import com.dmg.sp.service.SysUserService;
import com.dmg.sp.utils.Result;
import com.dmg.sp.vo.IdVo;
import com.dmg.sp.vo.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {


    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取所有用户
     * @return
     */
    @PostMapping("/getUserList")
    public Result getUserList(){
        List<User> list=sysUserService.getUserList();
        return Result.success(list);
    }

    /**
     * 为用户分配角色
     * @return
     */
    @PostMapping("/setUserRole")
    public Result setUserRole(@RequestBody UserRoleVo vo){
        sysUserService.setUserRole(vo);
        return Result.success();
    }


    /**
     * 添加用户
     * @return
     */
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user){
        sysUserService.addUser(user);
        return Result.success();
    }


    /**
     * 获取当前登录人信息
     * @return
     */
    @PostMapping("/getUserInfo")
    public Result getUserInfo(){
        //从认证信息上下文中 获取用户权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        MyUserDetails myUserDetails =(MyUserDetails) authentication.getPrincipal();
        User user = myUserDetails.getUser();
        return Result.success(user);
    }


    /**
     * 查看用户对应的角色id
     * @return
     */
    @PostMapping("/getUserRoleIds")
    public Result getUserRoleIds(@RequestBody IdVo vo){
        return Result.success(sysUserService.getUserRoleIds(vo.getUserId()));
    }
}
